/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.FontMetrics;
import java.util.ArrayList;

/**
 *
 * @author menderleit
 */
public abstract class StringUtils {

    static ArrayList<String> wrapText(String text, int pageWidth, FontMetrics fm) {
        ArrayList<String> lines = new ArrayList();

        char[] chars = text.toCharArray();
        String s = "";
        String word = "";
        
        for (int i = 0; i < chars.length; i++) {
            if(chars[i] == '\n') {
                s += word;
                word = "";
                lines.add(s);
                s = "";
            } else {
                word += chars[i];
                if(chars[i] == ' ') {
                    s += word;
                    word = "";
                }
            }
            
            if(fm.stringWidth(s) + fm.stringWidth(word) > pageWidth) {
                lines.add(s);
                s = "";
            }
        }
        s += word;
        if(!s.equals("")) {
            lines.add(s);
        }
        
        return lines;
    }
}
